package cn.edu.neusoft.zengweidong707.neufood.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.edu.neusoft.zengweidong707.neufood.R;
import cn.edu.neusoft.zengweidong707.neufood.adapter.UserCommentAdapter;
import cn.edu.neusoft.zengweidong707.neufood.bean.FoodByIdBean;
import cn.edu.neusoft.zengweidong707.neufood.bean.IsCollectedBean;
import cn.edu.neusoft.zengweidong707.neufood.bean.IsSuccessBean;
import cn.edu.neusoft.zengweidong707.neufood.bean.UserOrderBean;
import cn.edu.neusoft.zengweidong707.neufood.listener.ListListener;
import cn.edu.neusoft.zengweidong707.neufood.listener.ZwdListener;
import cn.edu.neusoft.zengweidong707.neufood.model.UserModel;

public class FoodDetailsActivity extends BaseActivity {
    private ImageView buy_foodpic;
    private TextView buy_foodname;
    private TextView buy_foodintro;
    private TextView buy_price;
    private TextView buy_comment;
    private ImageButton collect_button;
    private Button buy_button;
    private String foodName,foodPic;
    private Double foodPrice;
    private int foodId;
    private UserModel model;
    private List<UserOrderBean> list;
    private LinearLayoutManager linearLayoutManager;
    private RecyclerView recyclerView;
    private UserCommentAdapter userCommentAdapter;
    private ZwdListener<FoodByIdBean> listener;
    private ZwdListener<IsSuccessBean> collectListener;
    private ZwdListener<IsCollectedBean> isCollectedListener;
    private ListListener<UserOrderBean> userCommentListener;
    private String flag;
    private String success;

    @Override
    void initView() {
        setLayout_file(R.layout.activity_food_details);
        buy_foodpic = (ImageView) findViewById(R.id.buy_foodpic);
        buy_foodname = (TextView) findViewById(R.id.buy_foodname);
        buy_foodintro = (TextView) findViewById(R.id.buy_foodintro);
        buy_price = (TextView) findViewById(R.id.buy_price);
        buy_comment = (TextView) findViewById(R.id.buy_comment);
        buy_button = (Button) findViewById(R.id.buy_button);
        collect_button = (ImageButton) findViewById(R.id.collect_button);
        recyclerView = (RecyclerView)findViewById(R.id.comment_rec);
        linearLayoutManager = new LinearLayoutManager(FoodDetailsActivity.this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }

    @Override
    void initEvent() {
        listener = new ZwdListener<FoodByIdBean>() {
            @Override
            public void onResponse(FoodByIdBean l) {

                buy_foodname.setText(l.getFoodname().toString());
                foodName = l.getFoodname().toString();

                Double prices = l.getPrice();
                foodPrice = prices;

                buy_price.setText(prices.toString());
                buy_foodintro.setText(l.getIntro().toString());
                String pic = l.getPic();
                foodPic = pic;
                if(pic == ""){
                    buy_foodpic.setImageResource(R.mipmap.ic_launcher);
                }
                else{
                    Picasso.with(FoodDetailsActivity.this)
                            .load(pic)
                            .into(buy_foodpic);
                }
            }

            @Override
            public void onFail(String msg) {
                showToast("failed");
            }
        };

        isCollectedListener = new ZwdListener<IsCollectedBean>() {
            @Override
            public void onResponse(IsCollectedBean l) {
                flag = l.getCollected();
                if("1".equals(flag)){
                    collect_button.setImageResource(R.drawable.ic_collect_press);
                } else {
                    collect_button.setImageResource(R.drawable.ic_collect_normal);
                }
            }

            @Override
            public void onFail(String msg) {
                showToast("Failed");
            }
        };

        userCommentListener = new ListListener<UserOrderBean>() {

            @Override
            public void onResponse(List<UserOrderBean> userOrderBeans) {
                list = userOrderBeans;
                userCommentAdapter = new UserCommentAdapter(FoodDetailsActivity.this, list);
                recyclerView.setAdapter(userCommentAdapter);
            }

            @Override
            public void onFail(String msg) {
                showToast("Failed");
            }
        };

        buy_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(FoodDetailsActivity.this,FoodOrderActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt("foodid",foodId);
                bundle.putString("foodname",foodName);
                bundle.putDouble("foodprice",foodPrice);
                bundle.putString("foodpic",foodPic);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

        collectListener = new ZwdListener<IsSuccessBean>() {
            @Override
            public void onResponse(IsSuccessBean l) {
                success = l.getSuccess();
                if ("1".equals(success)) {

                    if ("1".equals(flag)) {
                        collect_button.setImageResource(R.drawable.ic_collect_normal);
                        flag = "0";
                        showToast("取消收藏");
                    } else {
                        collect_button.setImageResource(R.drawable.ic_collect_press);
                        flag = "1";
                        showToast("收藏成功");
                    }
                } else {
                    showToast("收藏失败");
                }
            }

            @Override
            public void onFail(String msg) {
                showToast("Failed");
            }
        };

        collect_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Bundle bundle = getIntent().getExtras();
                foodId = bundle.getInt("foodid");
                int userid = Integer.parseInt(getUserid());
                model = new UserModel();

                model.userCollectionFood(userid, foodId, collectListener);


            }
        });
    }

    @Override
    void initData() {
        Bundle bundle = getIntent().getExtras();
        foodId = bundle.getInt("foodid");
        int userid = Integer.parseInt(getUserid());
        model = new UserModel();
        model.getFoodByID(foodId, listener);
        model.foodCollectionFlag(userid, foodId, 1, isCollectedListener);
        model.getUserComment(foodId, userCommentListener);
    }

}
